/**
 * 
 */
package vehicleController;

import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import validator.Validator;
import vehicleEntity.PurchasedVehicleEntity;
import vehicleLogic.MoveLogic;
import constants.Constants;

/**
 * @author Yokesh Kumar
 *
 */
public class MoveVehicleController implements Initializable{


	@FXML
	private TextField searchVId;
	@FXML
	private ComboBox<String> moveFromTo;
	@FXML
	private Label message;
	@FXML
	private TableView<PurchasedVehicleEntity> moveTable;
	@FXML
	private TableColumn<PurchasedVehicleEntity,Integer> vehicleId;
	@FXML
	private TableColumn<PurchasedVehicleEntity,String> regNo;
	@FXML
	private TableColumn<PurchasedVehicleEntity,String> vehicleType;
	@FXML
	private TableColumn<PurchasedVehicleEntity,String> vehicleName;
	@FXML
	private TableColumn<PurchasedVehicleEntity,Integer> years;
	@FXML
	private TableColumn<PurchasedVehicleEntity,Date> purDate;
	@FXML
	private Button moveButton;



	private static ObservableList<PurchasedVehicleEntity> vehicleList;

	/**
	 * This method gets the move from and to values
	 * @param event
	 * @Pre  retrieves the values to be set for move from and to fields
	 * @Post based on constants the values are passed to MoveVehicleLogic class
	 * @return none
	 *
	 */
	@FXML
	public void fetchMoveDetails(ActionEvent event){
		message.setText("");
		message.setTextFill(Color.RED);
		if(moveFromTo.getValue() != null && !moveFromTo.getValue().isEmpty()){
			MoveLogic logic = new MoveLogic();
			vehicleList = null;
			if(moveFromTo.getValue().equalsIgnoreCase(Constants.PURCHASE_TO_RENT)){
				vehicleList = logic.fetchVehicles(Constants.PURCHASE);
				populateTable();
				moveTable.setVisible(true);
				moveButton.setVisible(true);
				if(vehicleList == null ||vehicleList.size() == 0){
					message.setText("No vehicles available to move from purchase to rent");
					moveTable.setVisible(false);
					moveButton.setVisible(false);
				}
			}
			else if(moveFromTo.getValue().equalsIgnoreCase(Constants.RENT_TO_SALE)){
				vehicleList = logic.fetchVehicles(Constants.RENT);
				populateTable();
				moveTable.setVisible(true);
				moveButton.setVisible(true);
				if(vehicleList == null || vehicleList.size() == 0){
					message.setText("No vehicles Older than 10 years are available to move");
					moveTable.setVisible(false);
					moveButton.setVisible(false);
				}
			}
			else if(moveFromTo.getValue().equalsIgnoreCase(Constants.SALE_TO_RENT)){
				vehicleList = logic.fetchVehicles(Constants.SALE);
				populateTable();
				moveTable.setVisible(true);
				moveButton.setVisible(true);
				if(vehicleList == null ||vehicleList.size() == 0){
					message.setText("No vehicles available to move from sale to rent");
					moveTable.setVisible(false);
					moveButton.setVisible(false);
				}
			}
			else{
				message.setText("please select a move option");
			}		
		}
		else{
			message.setText("please select a move option");
		}
	}

	/**
	 * This method provides the options to move fields from purchase, sale and rent tables 
	 * @param event
	 * @Pre Constant values are set as the options
	 * @Post Based on the constants the values are then passed to the MoveVehicleLogic class
	 * @return none
	 * 
	 */
	@FXML
	public void moveVehicle(ActionEvent event){
		message.setText("");
		message.setTextFill(Color.RED);
		if(moveTable.getSelectionModel().getSelectedItems().size() != 0){
			vehicleList = moveTable.getSelectionModel().getSelectedItems();
			if(moveFromTo.getValue() != null && !moveFromTo.getValue().isEmpty()){
				MoveLogic logic = new MoveLogic();
				if(moveFromTo.getValue().equalsIgnoreCase(Constants.PURCHASE_TO_RENT)){
					String msg = logic.movePurchasetoRent(vehicleList);
					message.setTextFill(Color.GREEN);
					message.setText(msg);
					
					vehicleList = logic.fetchVehicles(Constants.PURCHASE);
					populateTable();
				}
				else if(moveFromTo.getValue().equalsIgnoreCase(Constants.RENT_TO_SALE)){
					String msg = logic.moveRentToSale(vehicleList);
					message.setTextFill(Color.GREEN);
					message.setText(msg);
					vehicleList = logic.fetchVehicles(Constants.RENT);
					populateTable();
				}
				else if(moveFromTo.getValue().equalsIgnoreCase(Constants.SALE_TO_RENT)){
					String msg = logic.moveSaleToRent(vehicleList);
					message.setTextFill(Color.GREEN);
					message.setText(msg);
					vehicleList = logic.fetchVehicles(Constants.SALE);
					populateTable();
				}
				else{
					message.setText("please select a move option");
				}		
			}
			else{
				message.setText("please select a move option");
			}
		}
		else{
			message.setText("Please select the vehicle to be moved");
		}
	}

	/**
	 * This method searches vehicle based on the option selected 
	 * @param event
	 * @Pre vehicle Id is retrieved based on the option provided in the list
	 * @Post vehicle ID along with constants is then passed to the MoveVehicleLogic class
	 * @return none
	 */
	@FXML
	public void searchVehicle(ActionEvent event){
		message.setText("");
		message.setTextFill(Color.RED);
		if(moveFromTo.getValue() != null && !moveFromTo.getValue().isEmpty()){
			if(searchVId != null & !searchVId.getText().isEmpty()){
				if(Validator.validateStringContainsInteger(searchVId.getText().trim())){
					MoveLogic logic = new MoveLogic();
					int id = new Integer(searchVId.getText().trim());
					if(moveFromTo.getValue().equalsIgnoreCase(Constants.PURCHASE_TO_RENT)){
						vehicleList = logic.searchVehicle(id,Constants.PURCHASE);
						if(vehicleList == null || vehicleList.size() == 0){
							message.setText("vehicle not available for Move from Purchase");
							moveTable.setVisible(false);
							moveButton.setVisible(false);
						}
						else{
						populateTable();
						moveTable.setVisible(false);
						
						moveButton.setVisible(true);
						}
					}
					else if(moveFromTo.getValue().equalsIgnoreCase(Constants.RENT_TO_SALE)){
						vehicleList = logic.searchVehicle(id,Constants.RENT);
						if(vehicleList == null || vehicleList.size() == 0){
							message.setText("vehicle not available for Move from Rent");
							moveTable.setVisible(false);
							moveButton.setVisible(false);
						}
						else{
							populateTable();
							moveTable.setVisible(true);
							moveButton.setVisible(true);
						}
						
					}
					else if(moveFromTo.getValue().equalsIgnoreCase(Constants.SALE_TO_RENT)){
						vehicleList = logic.searchVehicle(id,Constants.SALE);
						if(vehicleList == null || vehicleList.size() == 0){
							message.setText("vehicle not available for Move from Sale");
							moveTable.setVisible(false);
							moveButton.setVisible(false);
						}
						else{
							populateTable();
							moveTable.setVisible(true);
							moveButton.setVisible(true);	
						}
						
					}
					else{
						message.setText("please select a move option");
					}		
				}
				else{
					message.setText("Vehicle Id can contain only Numbers");
				}
			}
			else{
				message.setText("Please enter the Vehicle Id for search");
			}
		}
		else{
			message.setText("please select a move option");
		}	
	}

	/**
	 * This methods populate the values inside the table column fields
	 * @Param none
	 * @Pre Table field Values are retrieved
	 * @Post Table field Values are set inside the table
	 * @return none
	 */
	public void populateTable(){
		if(vehicleList != null && vehicleList.size() >0){
			int x = 0;
			vehicleId.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleEntity, Integer>("vehicleId"));
			regNo.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleEntity, String>("regNo"));
			vehicleType.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleEntity, String>("vehicleType"));
			vehicleName.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleEntity, String>("vehicleName"));
			years.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleEntity, Integer>("yearsOld"));
			purDate.setCellValueFactory(new PropertyValueFactory<PurchasedVehicleEntity, Date>("purchasedDate"));
			moveTable.getItems().setAll(vehicleList);
		}
		else{
			moveTable.getItems().clear();
		}
	}

	@FXML
	public void purchaseToRent(ActionEvent event){
		// search screen load
	}



	@FXML
	public void movePurchaseToRent(ActionEvent event){

	}

	@FXML
	public void saleToRent(ActionEvent event){
		// search screen load
	}

	@FXML
	public void searchSale(ActionEvent event){

	}

	@FXML
	public void moveSaleToRent(ActionEvent event){

	}


	@FXML
	public void rentToSale(ActionEvent event){
		// search screen load

	}

	@FXML
	public void searchRent(ActionEvent event){

	}

	@FXML
	public void moveRentToSale(ActionEvent event){

	}

	/* (non-Javadoc)
	 * @see javafx.fxml.Initializable#initialize(java.net.URL, java.util.ResourceBundle)
	 */
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		Validator.addTextLimiter(searchVId, 6);
	}
}
